/*
 * Daniel R Padilla
 *
 * Copyright (c) 2007, Daniel R Padilla
 *
 * This copyrighted material is made available to anyone wishing to use, modify,
 * copy, or redistribute it subject to the terms and conditions of the GNU
 * Lesser General Public License, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this distribution; if not, write to:
 * Free Software Foundation, Inc.
 * 51 Franklin Street, Fifth Floor
 * Boston, MA  02110-1301  USA
 */
package org.databasefacade.table.column;

import org.databasefacade.table.Table;
import org.databasefacade.table.column.datatype.DataType;
import org.databasefacade.translator.Translator;


/**
 * 
 * 
 */
public class ColumnMetaData  {
    String tableName = null;
    String tableAlias = null;
    String columnName = null;
    String columnLabel = null;
    boolean isRequired = false;
    boolean isUniqueField = false;
    boolean isIndexed = false;
    DataType dataType;
    Translator translator;

    
    /**
     * Is private and used for getting a copy
     */
    private ColumnMetaData() {
    	
    }
    
    public ColumnMetaData(Table table, String colName, DataType dt) {
    	this.columnName = colName;
    	this.tableName = table.getTableName();
    	this.tableAlias = table.getTableAlias();
    	this.dataType=dt;
    	this.translator = table.getDBHelper().getTranslator();
    }
    
    public ColumnMetaData(ColumnMetaData cr) {  	
    	makeCopy(this, cr);
    }
       
    public void setDataType(DataType dt) {
    	dataType=dt;
    }
    
    public DataType getDataType() {
    	return dataType;
    }
          
    public void setTableName(String table) {
       this.tableName=table;
    }
    
    public String getTableName() {
        return this.tableName;
    }
    
    public void setTableAlias(String alias) {
        this.tableAlias=alias;
    }
    public String getTableAlias() {
        return this.tableAlias;
    }
    
    
    public String getColumnLabel() {
        return this.columnLabel;
    }
    
    public void setColumnName(String name) {
        this.columnName = name.toUpperCase();
        this.columnLabel = titleCase(this.columnName.toLowerCase());
    }

    public String getColumnName() {
        return this.columnName.toUpperCase();
    }
        
    public short getColumnType() {
    	return dataType.getColumnType();
    }

    public void setIsUniqueField(boolean isUniqueField) {
        this.isUniqueField = isUniqueField;
        if (this.isUniqueField) {
            setIsRequired(true);
        }
    }

    public boolean isUniqueField() {
        return this.isUniqueField;
    }

    public void setIsIndexed(boolean isIndexed) {
        this.isIndexed = isIndexed;
    }

    public boolean isIndexed() {
        return this.isIndexed;
    }

    public void setIsRequired(boolean isRequired) {
        this.isRequired = isRequired;
        if (this.isRequired == true) {
            dataType.setNullable("NOT NULL");
        } else {
        	dataType.setNullable("NULL");
        }
    }

    public boolean isRequired() {
        return this.isRequired;
    }
        
    public String getSqlType () {
    	return dataType.getSqlType();
    }
    
    public String getJavaType () {
    	return dataType.getJavaType();
    }
    
    public int getLength() {
    	return dataType.getLength();
    }
    
    public int getPrecision() {
    	return dataType.getPrecision();
    }
    
    public int getScale() {
    	return dataType.getScale();
    }
        
    public ColumnMetaData getCopy() {
    	ColumnMetaData cmd = new ColumnMetaData();
    	makeCopy(cmd, this);
    	return cmd;
    }
    
    public void makeCopy(ColumnMetaData newCmd, ColumnMetaData oldCmd) {
        if (oldCmd.getTableName() != null) {
        	newCmd.tableName = new String(oldCmd.getTableName());
        }
        if (oldCmd.getTableAlias() != null) {
        	newCmd.tableAlias = new String(oldCmd.getTableAlias());
        }
        if (oldCmd.getColumnName() != null) {
        	newCmd.columnName = new String(oldCmd.getColumnName());
        }
        if (oldCmd.getColumnLabel() != null) {
        	newCmd.columnLabel = new String(oldCmd.getColumnLabel());
        }
        newCmd.isRequired = oldCmd.isRequired;
        newCmd.isUniqueField = oldCmd.isUniqueField;
        newCmd.isIndexed = oldCmd.isIndexed; 
        newCmd.dataType = oldCmd.dataType.getCopy(translator);
        newCmd.translator = translator;
        
        if (newCmd.isUniqueField) {
        	newCmd.isRequired = true;
        }
    }
    
    

        
    private String titleCase(String colName) {
        boolean toUpper = true;
        String formattedText = "";

        for (int i = 0; i < colName.length(); i++) {
            String ch = "";

            if (i < colName.length() - 1) {
                ch = colName.substring(i, i + 1);
            } else {
                ch = colName.substring(i);
            }
            if (ch.equals("_")) {
                toUpper = true;
                formattedText += " ";
            } else {
                if (toUpper) {
                    formattedText += ch.toUpperCase();
                    toUpper = false;
                } else {
                    formattedText += ch;
                }
            }
    		
        }
        return formattedText;
    }
    
}
